package com.app.community.controller;

import com.app.community.dto.NotificationDTO;
import com.app.community.enums.NotificationStatusEnum;
import com.app.community.exception.ErrorCodeEnum;
import com.app.community.model.entity.Notification;
import com.app.community.service.AvatarService;
import com.app.community.service.NotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * created by czy on 2020/11/19 23:05
 **/
@Slf4j
@Controller
public class NotificationController {

    @Autowired
    HttpSession httpSession;

    @Autowired
    NotificationService notificationService;

    @Autowired
    AvatarService avatarService;

    @GetMapping("notification/{type}")
    public String getNotification(Model model, @PathVariable("type") String type) {
        // 根据用户的id进行定位
        String userId = (String) httpSession.getAttribute("userId");

        model.addAttribute("type",type);
        List<NotificationDTO> notifications = notificationService.selectNotification(userId, Integer.parseInt(type));
        model.addAttribute("notifications", notifications);
        return "common/notification::notificaitonFragment";
    }

    @GetMapping("notification")
    public String getNotification(Model model) {
        // 根据用户的id进行定位
        String userId = (String) httpSession.getAttribute("userId");

        if (userId == null || userId.isEmpty()) {
            model.addAttribute("error", ErrorCodeEnum.HAVENOTLOGIN.getDescription());
            model.addAttribute("signal",1);
            return "common/notification";
        }

        // 添加头像
        model.addAttribute("avatar",avatarService.selectUserAvatar(userId));
        // 默认查询点赞通知
        List<NotificationDTO> notifications = notificationService.selectNotification(userId,1);
        model.addAttribute("type",1);
        model.addAttribute("notifications", notifications);
        notificationService.updateNotificationStatus(userId);

        // 默认查询点赞通知
        return "common/notification";
    }
}
